<?php

namespace backend\controllers;

use Yii;
use backend\models\Logs;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use backend\models\User;
use backend\models\Goods;
/**
 * LogsController implements the CRUD actions for Logs model.
 */
class LogsController extends Controller
{
    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
//        return [
//            \backend\components\PermissionBehavior::className(),
//
//        ];
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
            'access'=>[
                'class'=> \backend\components\PermissionBehavior::className(),

            ]
        ];
    }

    /**
     * Lists all Logs models.
     * @return mixed
     */
    public function actionIndex()
    {
        $myrole=User::getmyrole();
        $userid=Yii::$app->user->id;
        $oprs=[];
        if($myrole=='一级审批人'){
            $allopr=Goods::find()->distinct('id')->leftJoin('user','user.id=goods.gys')->where(['goods.nextid'=>$userid])->select("user.id as id,user.realname as realname")->asArray()->all();

        }elseif($myrole=='二级审批人'){
            $allopr=User::find()->where(['parentid'=>$userid])->select('id,realname')->asArray()->all();

        }else{
            $allopr=User::find()->where(['<>','id',2])->asArray()->all();
        }
        $oprs['']='请选择';
        $oprs[$userid]=Yii::$app->user->identity->realname;

        foreach($allopr as $item){

            $oprs[$item['id']] = $item['realname'];
        }


        $searchModel = new LogsSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        $logssearch=[];
        if(!empty(Yii::$app->request->queryParams) && isset(Yii::$app->request->queryParams['LogsSearch'])){
            $logssearch = Yii::$app->request->queryParams['LogsSearch'];
        }


        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
            'logssearch' => $logssearch,
            'oprs'=>$oprs
        ]);
    }



    /**
     * Finds the Logs model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Logs the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Logs::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }
}
